package cn.fl.riskctrl.service.impl;


import cmm.mid.core.framework.dto.BaseParamDTO;
import cmm.mid.core.framework.dto.DataResultDTO;
import cmm.mid.core.framework.page.DataResultVO;
import cmm.user.facade.system.api.ISysUsrApiService;
import cmm.user.facade.system.dto.SysUsrDTO;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.DateUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.riskctrl.brdb.transform.*;
import cn.fl.riskctrl.brdb.vo.*;
import cn.fl.riskctrl.service.IChannelRatingService;
import cn.fl.riskctrl.service.IRcCstBscInfoService;

import cn.fl.riskctrl.ysdb.transform.YsejdCpwsVOTransform;
import cn.fl.riskctrl.ysdb.transform.YsejdEntryDVOTransform;
import cn.fl.riskctrl.ysdb.transform.YsejdReportVOTransform;
import cn.fl.riskctrl.ysdb.transform.YsejdZxggVOTransform;
import cn.fl.riskctrl.ysdb.vo.*;
import com.alibaba.fastjson.JSON;
import fl.riskctrl.facade.brdb.dto.BrApplyDTO;
import fl.riskctrl.facade.brdb.dto.BrcdApplyDTO;
import fl.riskctrl.facade.brdb.dto.BrcjwsReportDTO;
import fl.riskctrl.facade.brjf.condition.BRJFCreditRecordQC;
import fl.riskctrl.facade.externalinterface.service.api.IExternalChannelRatingApiService;
import fl.riskctrl.facade.ysdb.dto.YsejdApplyDTO;
import fl.riskctrl.facade.ysdb.dto.YsejdCpwsPartyDTO;
import fl.riskctrl.facade.ysdb.dto.YsejdEntryDDTO;
import fl.riskctrl.facade.ysdb.dto.YsejdReportDTO;
import fl.riskctrl.facade.yszx.condition.YsEntryQC;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


/**
 * @Description: 风险控制客户基础信息表 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class ChannelRatingServiceImpl implements IChannelRatingService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(IRcCstBscInfoService.class);

	@Autowired
    private IExternalChannelRatingApiService externalChannelRatingApiService;
	@Autowired
    private ISysUsrApiService sysUsrApiService;


    @Override
    public DataResultVO getBrcdListReport(String  dataId,String dataType) throws BaseException {
        BaseLogUtils.info(logger, "getBrcdListReport", "获取百融报告列表，请求参数：" + dataId, CurrentThreadContext.getCurrentUserName());
        DataResultVO dataResultVO = new DataResultVO(BaseConstant.IS_YESNO_YES, "修改成功!");
        try {
            if (StringUtils.isEmpty(dataId)) {
                BaseLogUtils.info(logger, "modify", "修改对象不能为空!");
                throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "修改对象不能为空!");
            }
            BRJFCreditRecordQC qc =new BRJFCreditRecordQC();
            qc.setDataId(Long.valueOf(dataId));
            qc.setDataType(dataType);
            BaseParamDTO baseParamDto = new BaseParamDTO();
            DataResultDTO dataResultDTO = this.externalChannelRatingApiService.getBrcdReportList(qc, baseParamDto);
            if (dataResultDTO.getSuccess() != BaseConstant.IS_YESNO_YES) {
                BaseLogUtils.error(logger, "getBrcdListReport", dataResultDTO.getInfo());
                throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_603, dataResultDTO.getInfo());
            }
            List<BrcdApplyDTO> dtoList=(List<BrcdApplyDTO>)dataResultDTO.getData();
            List<BrcdApplyVO> voList=null;
            if(CollectionUtils.isNotEmpty(dtoList)){
                voList=new ArrayList<BrcdApplyVO>();
                for (BrcdApplyDTO brcdApplyDTO:dtoList){
                    BrcdApplyVO brcdApplyVO=new BrcdApplyVO();
                    brcdApplyVO.setUsrNm(getUserName(brcdApplyDTO.getCrtUsrId()));
                    brcdApplyVO.setCrtTm(brcdApplyDTO.getCrtTm());
                    brcdApplyVO.setReportNo(brcdApplyDTO.getReportNo());
                    brcdApplyVO.setBrApplyId(brcdApplyDTO.getBrApplyId());
                    brcdApplyVO.setEndTim(DateUtils.getDateAddDate(brcdApplyDTO.getCrtTm(), 2, -24));
                    voList.add(brcdApplyVO);
                }
            }
            dataResultVO.setInfo(dataResultDTO.getInfo());
            dataResultVO.setSuccess(dataResultDTO.getSuccess());
            dataResultVO.setData(voList);
        } catch (BaseException e) {
            throw e;
        } catch (Exception e) {
            BaseLogUtils.error(logger, "modify", "获取百融报告列表方法失败", e);
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_603, "获取百融报告列表方法失败");
        }
        BaseLogUtils.info(logger, "getBrcdListReport", "获取百融报告列表结束，返回结果：" + JSON.toJSONString(dataResultVO));
        return dataResultVO;
    }

    @Override
    public DataResultVO getBrApplyReportDetail(String  reportNo,String brApplyId) throws BaseException {
        BaseLogUtils.info(logger, "getBrApplyReportDetail", "获取百融报告详细信息，请求参数：" + reportNo+"  BrapplyId:"+brApplyId, CurrentThreadContext.getCurrentUserName());
        DataResultVO dataResultVO = new DataResultVO(BaseConstant.IS_YESNO_YES, "修改成功!");
        try {
            if (StringUtils.isEmpty(reportNo)) {
                BaseLogUtils.info(logger, "modify", "报告ID不能为空!");
                throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "报告ID不能为空!");
            }
            if (StringUtils.isEmpty(brApplyId)) {
                BaseLogUtils.info(logger, "modify", "百融申请ID不能为空!");
                throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "百融申请ID不能为空!");
            }
            BRJFCreditRecordQC qc =new BRJFCreditRecordQC();
            qc.setReportNo(Long.valueOf(reportNo));
            qc.setBrApplyId(Long.valueOf(brApplyId));
            BaseParamDTO baseParamDto = new BaseParamDTO();
            DataResultDTO dataResultDTO = this.externalChannelRatingApiService.getBrcdReportDetail(qc, baseParamDto);
            if (dataResultDTO.getSuccess() != BaseConstant.IS_YESNO_YES) {
                BaseLogUtils.error(logger, "getBrApplyReportDetail", dataResultDTO.getInfo());
                throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_603, dataResultDTO.getInfo());
            }
            BrApplyDTO brApplyDTO=(BrApplyDTO)dataResultDTO.getData();
            BrApplyVO brApplyVO=BrApplyVOTransform.toVO(brApplyDTO);
            if(brApplyDTO.getBrcdReportDTO()!=null){
                BrcdReportVO brcdReportVO = BrcdReportVOTransform.toVO(brApplyDTO.getBrcdReportDTO());
                brApplyVO.setBrcdReportVO(brcdReportVO);
                if(brApplyDTO.getBrcdReportDTO().getBrcdCpwsDTOList()!=null){
                    List<BrcdCpwsVO> brcdCpwsVOlist= BrcdCpwsVOTransform.toVoList(brApplyDTO.getBrcdReportDTO().getBrcdCpwsDTOList());
                    brApplyVO.getBrcdReportVO().setBrcdCpwsVOList(brcdCpwsVOlist);
                }
                if(brApplyDTO.getBrcdReportDTO().getBrcdZxggDTOList()!=null){
                    List<BrcdZxggVO> brcdZxggVOList= BrcdZxggVOTransform.toVoList(brApplyDTO.getBrcdReportDTO().getBrcdZxggDTOList());
                    brApplyVO.getBrcdReportVO().setBrcdZxggVOList(brcdZxggVOList);
                }
                if(brcdReportVO.getCrtTm()!=null) {
                    brApplyVO.setCrtTm(brcdReportVO.getCrtTm());
                    brApplyVO.setEndTm(DateUtils.getDateAddDate(brcdReportVO.getCrtTm(), 2, -24));
                }
            }
            if (brApplyDTO.getBrcjwsReportDTOList() != null){
                List<BrcjwsReportVO> brcjwsReportVOList = BrcjwsReportVOTransform.toVOList(brApplyDTO.getBrcjwsReportDTOList());
                brApplyVO.setBrcjwsReportVOList(brcjwsReportVOList);
            }
            dataResultVO.setInfo(dataResultDTO.getInfo());
            dataResultVO.setSuccess(dataResultDTO.getSuccess());
            dataResultVO.setData(brApplyVO);
        } catch (BaseException e) {
            throw e;
        } catch (Exception e) {
            BaseLogUtils.error(logger, "modify", "获取百融报告详细信息方法失败", e);
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_603, "获取百融报告详细信息方法失败");
        }
        BaseLogUtils.info(logger, "getBrApplyReportDetail", "获取百融报告详细信息结束，返回结果：" + JSON.toJSONString(dataResultVO));
        return dataResultVO;
    }


    @Override
    public DataResultVO getYsedjListReport(String  dataId,String dataType) throws BaseException {
        BaseLogUtils.info(logger, "getBrcdListReport", "获取元素报告列表，请求参数：" + dataId, CurrentThreadContext.getCurrentUserName());
        DataResultVO dataResultVO = new DataResultVO(BaseConstant.IS_YESNO_YES, "修改成功!");
        try {
            if (StringUtils.isEmpty(dataId)) {
                BaseLogUtils.info(logger, "modify", "修改对象不能为空!");
                throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "修改对象不能为空!");
            }
            YsEntryQC qc =new YsEntryQC();
            qc.setDataId(Long.valueOf(dataId));
            qc.setDataType(dataType);
            BaseParamDTO baseParamDto = new BaseParamDTO();
            DataResultDTO dataResultDTO = this.externalChannelRatingApiService.getYsejdReportList(qc, baseParamDto);
            if (dataResultDTO.getSuccess() != BaseConstant.IS_YESNO_YES) {
                BaseLogUtils.error(logger, "getYsedjListReport", dataResultDTO.getInfo());
                throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_603, dataResultDTO.getInfo());
            }
            List<YsejdApplyDTO> dtoList=(List<YsejdApplyDTO>)dataResultDTO.getData();
            List<YsejdApplyVO> voList=null;
            if(CollectionUtils.isNotEmpty(dtoList)){
                voList=new ArrayList<YsejdApplyVO>();
                for (YsejdApplyDTO ysejdApplyDTO:dtoList){
                    YsejdApplyVO ysejdApplyVO=new YsejdApplyVO();
                    ysejdApplyVO.setUsrNm(getUserName(ysejdApplyDTO.getCrtUsrId()));
                    ysejdApplyVO.setCrtTm(ysejdApplyDTO.getCrtTm());
                    ysejdApplyVO.setReportNo(ysejdApplyDTO.getReportNo());
                    ysejdApplyVO.setYsApplyId(ysejdApplyDTO.getYsApplyId());
                    ysejdApplyVO.setEndDate(DateUtils.getDateAddDate(ysejdApplyVO.getCrtTm(), 2, -24));
                    voList.add(ysejdApplyVO);
                }
            }
            dataResultVO.setInfo(dataResultDTO.getInfo());
            dataResultVO.setSuccess(dataResultDTO.getSuccess());
            dataResultVO.setData(voList);
        } catch (BaseException e) {
            throw e;
        } catch (Exception e) {
            BaseLogUtils.error(logger, "modify", "获取元素报告列表方法失败", e);
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_603, "获取元素报告列表方法失败");
        }
        BaseLogUtils.info(logger, "getBrcdListReport", "获取元素报告列表结束，返回结果：" + JSON.toJSONString(dataResultVO));
        return dataResultVO;
    }
    @Override
    public DataResultVO getYsejdApplyReportDetail(String  reportNo,String ysApplyId) throws BaseException {
        BaseLogUtils.info(logger, "getYsejdApplyReportDetail", "获取元素企业司法数据报告详细信息，请求参数：" + reportNo+"  ysapplyId:"+ysApplyId, CurrentThreadContext.getCurrentUserName());
        DataResultVO dataResultVO = new DataResultVO(BaseConstant.IS_YESNO_YES, "修改成功!");
        Boolean flag=true;
        try {
            if (StringUtils.isEmpty(reportNo)) {
                BaseLogUtils.info(logger, "getYsejdApplyReportDetail", "报告ID不能为空!");
                throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "报告ID不能为空!");
            }
            if (StringUtils.isEmpty(ysApplyId)) {
                BaseLogUtils.info(logger, "getYsejdApplyReportDetail", "元素申请ID不能为空!");
                throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "元素申请ID不能为空!");
            }
            YsEntryQC qc =new YsEntryQC();
            qc.setReportNo(Long.valueOf(reportNo));
            qc.setYsApplyId(Long.valueOf(ysApplyId));
            BaseParamDTO baseParamDto = new BaseParamDTO();
            DataResultDTO dataResultDTO = this.externalChannelRatingApiService.getYsejdReportDetail(qc, baseParamDto);
            if (dataResultDTO.getSuccess() != BaseConstant.IS_YESNO_YES) {
                BaseLogUtils.error(logger, "getYsejdApplyReportDetail", dataResultDTO.getInfo());
                throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_603, dataResultDTO.getInfo());
            }
            YsejdReportDTO ysejdReportDTO=(YsejdReportDTO)dataResultDTO.getData();
            YsejdReportVO ysejdReportVO= YsejdReportVOTransform.toVO(ysejdReportDTO);
            if(ysejdReportVO.getCrtTm()!=null) {
                ysejdReportVO.setEndDate(DateUtils.getDateAddDate(ysejdReportVO.getCrtTm(), 2, -24));
            }
            List<YsejdZxggVO> ysejdZxggVOList=new ArrayList<YsejdZxggVO>();
            List<YsejdCpwsVO> ysejdCpwsVOList=new ArrayList<YsejdCpwsVO>();
            if(ysejdReportDTO.getYsejdEntryDDTOList()!=null){
                for (YsejdEntryDDTO ysejdEntryDDTO:ysejdReportDTO.getYsejdEntryDDTOList()) {
                    if("cpws".equals(ysejdEntryDDTO.getDataType())){
                        if(CollectionUtils.isNotEmpty(ysejdEntryDDTO.getYsejdCpwsPartyDTOList())) {
                            for (YsejdCpwsPartyDTO ysejdCpwsPartyDTO : ysejdEntryDDTO.getYsejdCpwsPartyDTOList()) {
                                if (StringUtils.isNotEmpty(ysejdCpwsPartyDTO.getTitle())) {
                                    flag=false;
                                    YsejdCpwsVO ysejdCpwsVO = YsejdCpwsVOTransform.toVO(ysejdEntryDDTO);
                                    ysejdCpwsVO.setCpwsTitle(ysejdCpwsPartyDTO.getTitle());
                                    ysejdCpwsVOList.add(ysejdCpwsVO);
                                }
                            }

                        }
                        if(flag){
                            ysejdCpwsVOList.add(YsejdCpwsVOTransform.toVO(ysejdEntryDDTO));
                        }
                    }
                    if("zxgg".equals(ysejdEntryDDTO.getDataType())){
                        ysejdZxggVOList.add(YsejdZxggVOTransform.toVO(ysejdEntryDDTO));
                    }

                }

                ysejdReportVO.setYsejdCpwsVOList(ysejdCpwsVOList);
                ysejdReportVO.setYsejdZxggVOList(ysejdZxggVOList);

            }
            dataResultVO.setInfo(dataResultDTO.getInfo());
            dataResultVO.setSuccess(dataResultDTO.getSuccess());
            dataResultVO.setData(ysejdReportVO);
        } catch (BaseException e) {
            throw e;
        } catch (Exception e) {
            BaseLogUtils.error(logger, "modify", "获取企业司法数据报告详细信息方法失败", e);
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_603, "获取企业司法数据报告详细信息方法失败");
        }
        BaseLogUtils.info(logger, "getBrApplyReportDetail", "获取企业司法数据报告详细信息结束，返回结果：" + JSON.toJSONString(dataResultVO));
        return dataResultVO;
    }

    private  String getUserName(Long usrId)throws BaseException{
        String usrName=null;
        if(usrId==null){
            return usrName;
        }
        if ( 1 == usrId){
            return "系统";
        }
        try {
            List<Long> usrIdList=new ArrayList<Long>();
            usrIdList.add(usrId);
            List<SysUsrDTO> sysUsrDTOS = sysUsrApiService.selectSysUsrList(usrIdList);
            if(CollectionUtils.isEmpty(sysUsrDTOS)){
                return usrName;
            }
            SysUsrDTO sysUsrDTO=sysUsrDTOS.get(0);
            usrName=sysUsrDTO.getUsrNm();
        } catch (BaseException e) {
            throw e;
        } catch (Exception e) {
            BaseLogUtils.error(logger, "modify", "修改矢量方法失败", e);
            throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_603, "修改矢量方法失败");
        }
       return usrName;
    }

}
